﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DataAccess.DataObject;
using System.Data;
using System.Collections.Generic;
using System.Globalization;

namespace EnmineKaitlin.admin
{
    public partial class poll_new : Common.PageBase
    {
        const string module = "manage-poll";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CheckPermission(module))
            {
                try
                {
                    if (!IsPostBack)
                    {
                        if (Request["p"] != null && Request["p"].ToString() != "")
                        {
                            lblFunction.Text = "Cập nhật thăm dò";
                            btnAdd.Text = "Cập nhật";
                            pnlAddPoll.Visible = false;
                            BindDetail(int.Parse(Request["p"].ToString()));
                        }
                        else
                        {
                            lblFunction.Text = "Thêm câu hỏi thăm dò ý kiến";
                            btnAdd.Text = "Bắt đầu thăm dò";
                            txtStartDate.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/admin/default.aspx");
                }
            }
            else
            {
                Response.Redirect("~/admin/permission-fail.aspx");
            }
        }
        protected void BindDetail(int PollID)
        {
            rptDetailPoll.DataSource = _BPoll.Get(PollID, 1, 10);
            rptDetailPoll.DataBind();            
        }        
        protected void btnAdd_Click(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    if (Request.Params["hdfNumberOption"] != null)
                    {
                        //add poll
                        _OPoll.PollTitle = txtTitle.Text;
                        _OPoll.Description = txtExcerpt.Text;
                        _OPoll.CreateDate = DateTime.Now;
                        _OPoll.StartDate = DateTime.Parse(txtStartDate.Text, culture, DateTimeStyles.NoCurrentDateDefault);
                        _OPoll.EndDate = DateTime.Parse(txtEndDate.Text, culture, DateTimeStyles.NoCurrentDateDefault);
                        _OPoll.Status = e.CommandArgument.ToString();
                        _OPoll.LangID = ddlLanguage.SelectedValue;
                        _OPoll.UserCreate = 2;
                        _OPoll.PollType = "normal";
                        _OPoll.MultiSelect = 0;
                        if (_BPoll.Add(_OPoll))
                        {
                            //add option
                            IList<OPoll> lstPool = _BPoll.GetLastRecord();
                            int PollId = lstPool[0].PollID;
                            int NumberOption = int.Parse(Request.Params["hdfNumberOption"].ToString());
                            string idOption = "";
                            for (int i = 0; i < NumberOption; i++)
                            {
                                idOption = "hdfOption" + i.ToString();
                                if (Request.Params[idOption] != null)
                                {
                                    _OOption.PollID = PollId;
                                    _OOption.OptionName = Request.Params[idOption].ToString();
                                    _OOption.OptionParent = 0;
                                    _OOption.OptionValue = "";
                                    _OOption.Description = "";
                                    _BOption.Add(_OOption);                                    
                                }
                            }
                            Response.Redirect("~/admin/poll-new.aspx?p=" + PollId);                            
                        }
                    }
                    else {
                        lblNotice.Text = "Bạn phải nhập ít nhất 1 lựa chọn !";
                    }
                    break;
                case "Update":
                    //update poll
                    if (Request["p"] != null && Request["p"].ToString() != "")
                    {
                        int PollID = int.Parse(Request["p"].ToString());
                        _OPoll.PollID = PollID;
                        _OPoll.PollTitle = ((TextBox)rptDetailPoll.Items[0].FindControl("txtTitle")).Text;
                        _OPoll.Description = ((TextBox)rptDetailPoll.Items[0].FindControl("txtExcerpt")).Text;
                        _OPoll.StartDate = DateTime.Parse(((TextBox)rptDetailPoll.Items[0].FindControl("txtStartDate")).Text, culture, DateTimeStyles.NoCurrentDateDefault);
                        _OPoll.EndDate = DateTime.Parse(((TextBox)rptDetailPoll.Items[0].FindControl("txtEndDate")).Text, culture, DateTimeStyles.NoCurrentDateDefault);
                        _OPoll.Status = ((DropDownList)rptDetailPoll.Items[0].FindControl("ddlStatus")).SelectedValue;
                        _OPoll.LangID = ((DropDownList)rptDetailPoll.Items[0].FindControl("ddlLanguage")).SelectedValue;
                        _OPoll.CreateDate = _BPoll.Get(PollID, 1, 1)[0].CreateDate;
                        _OPoll.PollType = "normal";
                        _OPoll.MultiSelect = 0;
                        if (_BPoll.Update(_OPoll))
                        {
                            Response.Redirect("~/admin/poll-new.aspx?p=" + PollID);
                        }
                    }
                    break;
            }
        }        
    }
}
